package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.RequiredLogin;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.StrategyCommentQueryObject;
import cn.wolfcode.wolf2w.query.StrategyQueryObject;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVoRedisService;
import cn.wolfcode.wolf2w.service.*;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("strategies")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyThemeService strategyThemeService;
    @Autowired
    private IStrategyCommentService strategyCommentService;
    @Autowired
    private IStrategyStatisVoRedisService statisVoRedisService;


    //分页查询
    @GetMapping("/query")
    public Object query(StrategyQueryObject qo){
        return JsonResult.success(strategyService.query(qo));
    }

    //攻略明细
    @GetMapping("/detail")
    public Object detail(String id) {
        Strategy strategy = strategyService.get(id);

        //阅读数+1
        statisVoRedisService.viewnumIncrease(id, 1);
        return JsonResult.success(strategy);
    }

    //查询主题
    @GetMapping("/themes")
    public Object themes(){
        return JsonResult.success(strategyThemeService.list());
    }

    @RequiredLogin
    @PostMapping("/addComment")
    public Object addComment(StrategyComment comment, @UserParam UserInfo userInfo){
        //设置用户信息,使用bean属性复制 前提:属性名字必须一致
        BeanUtils.copyProperties(userInfo, comment);
        comment.setUserId(userInfo.getId());
        strategyCommentService.save(comment);

        //评论数+1
        statisVoRedisService.replynumIncrease(comment.getStrategyId(),1);

        return JsonResult.success();
    }

    //攻略评论分页查询
    @GetMapping("/comments")
    public Object comments(StrategyCommentQueryObject qo){
        return JsonResult.success(strategyCommentService.query(qo));
    }

    //点赞
    @RequiredLogin
    @PostMapping("/commentThumb")
    public Object commentThumb(String cid,@UserParam UserInfo userInfo){

        strategyCommentService.commentThumb(cid,userInfo.getId());
        return JsonResult.success();
    }

    //攻略明细
    @GetMapping("/statisVo")
    public Object statisVo(String sid){
        return JsonResult.success(statisVoRedisService.getStrategyStatisVo(sid));
    }


    //攻略收藏
    @RequiredLogin
    @PostMapping("/favor")
    public Object favor(String sid, @UserParam UserInfo userInfo){
        boolean  ret = statisVoRedisService.favor(sid, userInfo.getId());
        return JsonResult.success(ret);
    }

    //攻略点赞
    @RequiredLogin
    @PostMapping("/strategyThumbup")
    public Object strategyThumbup(String sid, @UserParam UserInfo userInfo) {
        boolean ret = statisVoRedisService.strategyThumbup(sid, userInfo.getId());
        return JsonResult.success(ret);
    }
}
